/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.s21.poo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author pgomez
 */
public class MySQLConnection {
    
    private Connection conn;
    private static MySQLConnection db;
   
    private MySQLConnection() {
       
        String url= "jdbc:mysql://localhost:3306/";
        String dbName = "gestionardb";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "root";
        try {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(url+dbName,userName,password);
        }
        catch (ClassNotFoundException | SQLException sqle) {
            System.out.println("Error creando conexion:" + sqle.getMessage());
        }
    }
    /**
     *
     * @return MysqlConnect Database connection object
     */
    public static synchronized MySQLConnection getInstance() {
        if ( db == null ) {
            db = new MySQLConnection();
        }
        return db;
     }
    
    /**
     * @param sql
     * @return PreparedStatement
     * @throws SQLException 
     */
    public PreparedStatement preparedStatement(String sql) throws SQLException{
        return conn.prepareStatement(sql);
    }
    
    /**
     * @throws SQLException 
     */
    public void close() throws SQLException{
        this.conn.close();
   }
}
